package rsschart.charter;

import rsschart.feed.Category;
import rsschart.feed.DataSet;
import rsschart.feed.RssChart;

public class PieChart implements Charter {

	final static int TITLE_Y = 15;
	final static int CHART_X_START = 10;
	final static int CHART_TOP = 20;
	final static int Y_AXIS_SIZE = 200;
	final static int CHART_WIDTH = 200;
	final static int centerX = 110;
	final static int centerY = 120;
	final static int radius = 80;
	final static int labelRadius = 85;
	
	public String draw(RssChart chart) {
		// TODO Auto-generated method stub
		/*
		double startDegrees = 0;
		double endDegrees;
		double labelDegrees;
		double arcStartX;
		double arcEndX;
		double arcStartY;
		double arcEndY;
		*/
		double sliceDegrees = 0;
		double runningDegrees = 0;
		//String color;
		String values[];
		int numVals;
		double valueTotal = 0;
		
		StringBuffer chartSvg = new StringBuffer();
		int textCenter = 110;

		String link="./chartClick";
		String catgName;
		String  catgLabel; 
		String dataSetsName;
		String dataSetName;
		
		numVals = chart.getSeriesPointCount();
		
		//-- draw header svg
		chartSvg.append(CharterUtil.getHeader(chart.getWidth(), chart.getHeight()));
		chartSvg.append("<!-- end header info -->\n");
		
		chartSvg.append("\n<!-- gradient defs -->\n");
		chartSvg.append(CharterUtil.getGradient(numVals, CharterUtil.RADIAL_GRADIENT));
		
		//-- add javascript method for clicking:
		chartSvg.append("\n<!-- onclick method -->\n");
		chartSvg.append(CharterUtil.getClickMethod());

		//-- draw click instruction:
		chartSvg.append("\n<!-- click instruction -->\n");
		chartSvg.append(CharterUtil.clickNote());
		
		//-- draw the title
		chartSvg.append("\n<!-- title -->\n");
		chartSvg.append(CharterUtil.getTitle(textCenter, TITLE_Y, chart.getTitle()));

		//-- draw backframe
		chartSvg.append("\n<!-- chart background -->\n");
		chartSvg.append(CharterUtil.getChartFrame(CHART_X_START, CHART_TOP, CHART_WIDTH, Y_AXIS_SIZE));
 
		
		//-- do pie specifics:
		//-- work off the first DataSet:
		DataSet dataSet;
		//-- throw Exception if...?
		dataSet = chart.getDataSet(0);
		dataSetsName = chart.getDataSetsName();
		dataSetName = dataSet.getName();
		values = dataSet.getValue();
		//numVals = values.length;
		catgName = chart.getCategory().getName();
		//-- first category for now...
		catgLabel = chart.getCategory().getValue()[0];
			
		
		//-- loop through once to get the total value of all slices:
		for (int i=0;i<numVals; i++) {
			valueTotal += Double.valueOf(values[i]);
		}
		
		//-- loop through again to build each slice
		for (int i=0;i<numVals; i++) {
			//-- get current angle in degrees
			sliceDegrees = 360 * (Double.valueOf(values[i])/valueTotal);
			//-- pass current and running total to drawSlice
			chartSvg.append(drawSlice(sliceDegrees, 
									  runningDegrees, 
									  CharterUtil.c(i),
									  values[i],
									  i,
									  link,
									  catgName,
									  catgLabel, 
							          dataSetsName, 
							          dataSetName));
			runningDegrees+=sliceDegrees;
		}
		//-- draw legend
		chartSvg.append(drawLegend(chart, 220, 30));
		
		chartSvg.append(CharterUtil.getClose());
		return chartSvg.toString();
	}
	
	private double getX(double degrees, int r) {
		//-- rotate the effect so that zero degrees is at 12:00
		double adjDegrees;
		double xCoord;
		adjDegrees = 90-degrees;
		xCoord = Math.round(r*Math.cos(Math.PI * adjDegrees/180));
		return xCoord;
	}
	
	private double getY(double degrees, int r) {
		//-- rotate the effect so that zero degrees is at 12:00		
		double adjDegrees;
		double yCoord;
		adjDegrees = 90-degrees;
		yCoord = Math.round(r*Math.sin(Math.PI * adjDegrees/180));		
		return yCoord;
	}
	
	private String drawSlice(double current, 
							double running, 
							String color,
							String value,
							int setIndex,
				            String link, 
				            String catgName, 
				            String catgLabel, 
				            String dataSetsName, 
				            String dataSetName) {
		StringBuffer slice = new StringBuffer();
		double x;
		double y;
		 
		slice.append("<path d=\"M" + centerX + "," + centerY + " L");
		x = centerX + getX(running, radius);
		y = centerY - getY(running, radius);
		slice.append(x + "," + y ); //-- line to...
		slice.append(" A80,80 0 0,1 ");
		//-- end of arc
		x = centerX + getX(running+current, radius);
		y = centerY - getY(running+current, radius);
		slice.append(x + "," + y); //-- arc to 
		slice.append(" Z\" " + " fill=\"url(#c" + setIndex + ")\"" + 
				" style=\"stroke: black; stroke-width: 1\"/>\n");
		
		//-- label:
		//running+(current/2)
		x = centerX + getX(running+(current/2), labelRadius);
		y = centerY - getY(running+(current/2), labelRadius);
		slice.append("<text x=\"" + x + "\" y=\"" + y + "\" text-anchor=\"middle\" font-size=\"8\">");
		slice.append(value + "</text>\n");
		
		return slice.toString();
	}

	
	private String drawLegend(RssChart chart, int startX, int startY) {
		int MAX_NUM_SYMBOLS = 16;
		
		int legendX = startX;
		int legendY = startY;
		
		int symbolX = legendX + 2;
		int symbolY = legendY + 2;
		int symbolLabelX = legendX + 10;
		int symbolLabelY = legendY + 8;
		
		int legendWidth = 310-startX;
		int legendHeight;
		int setCount;
		
		String color;
		String label;
		Category catg = chart.getCategory();
		String[] values = catg.getValue();
		
		int valCount = values.length;
		
		legendHeight = valCount * 10;
		StringBuffer legend = new StringBuffer();
		
		legend.append("<text x=\"" + legendX + "\" y=\"" + (legendY) + "\" text-anchor=\"start\"");
		legend.append(" font-size=\"8\">");
		legend.append(catg.getName());
		legend.append("</text>");

		legend.append("<rect x=\"" + legendX + "\" y=\"" + legendY + "\" width=\"" + legendWidth +
				"\" height=\"" + legendHeight + "\" fill=\"none\" stroke=\"black\" stroke-width=\"0.25\"/>\n");
		
		for (int i=0; i<valCount; i++) {
			color = CharterUtil.c(i);
			legend.append("<rect x=\"" + symbolX + "\" y=\"" + symbolY + "\" width=\"6\"" + 
					" height=\"6\" fill=\"" + color + "\" stroke=\"none\" />\n");
			
			legend.append("<text x=\"" + symbolLabelX + "\" y=\"" + symbolLabelY + "\" text-anchor=\"start\"");
			legend.append(" font-size=\"6\">");
			legend.append(values[i]);
			legend.append("</text>");
			
			//-- move the pointer down to draw the next symbol
			symbolY+=10;
			symbolLabelY+=10;
		}
		return legend.toString();
		
	}

	
	public static void main(String[] args) {
		long someHex = 0xde4321;
		//System.out.println(someHex);
		someHex += 0xf4240;
		//System.out.println(Long.toHexString(someHex));
		
		StringBuffer bars = new StringBuffer();
		StringBuffer defs = new StringBuffer();
		String color;
		String endColor;
		String midColor;
		long midColorHex;
		long endColorHex;
		
		
		defs.append(CharterUtil.getGradient(16, CharterUtil.LINEAR_GRADIENT));
		
		int x=10;
		
		
		
		
		for (int i=0;i<16;i++) {
			
			/*
			color = CharterUtil.c(i);

			//endColorHex = Long.decode("0x" + color.substring(1)) + 0xf4240;
			endColorHex = Long.decode("0x" + color.substring(1)) + 0x333333;
			endColorHex = endColorHex >= 0xeeeeee ? 0xeeeeee : endColorHex;
			endColor = "#" + Long.toHexString(endColorHex);
			midColorHex = Long.decode("0x" + color.substring(1)) + 0x555555;
			midColorHex = midColorHex >= 0xffffff ? 0xffffff : midColorHex;
			midColor = "#" + Long.toHexString(midColorHex);
			
			defs.append("<linearGradient id=\"c" + i + "\" x1=\"0%\" y1=\"0%\" x2=\"100%\" y2=\"0%\">\n");
			defs.append("<stop offset=\"0%\" style=\"stop-color:" + color + ";stop-opacity:1\"/>\n");
			defs.append("<stop offset=\"65%\" style=\"stop-color:" +  midColor+ ";stop-opacity:1\"/>\n");
			defs.append("<stop offset=\"100%\" style=\"stop-color:" + color + ";stop-opacity:1\"/>\n");
			//defs.append("<stop offset=\"100%\" style=\"stop-color:" + endColor + ";stop-opacity:1\"/>\n");
			defs.append("</linearGradient>\n");
			*/
			
			bars.append("<rect x=\""+ x + "\" y=\"30\" width=\"30\" height=\"120\" stroke=\"none\""  + 
					" stroke-width=\"0\" style=\"fill:url(#c" + i + ")\"/>\n");
			x+=35;
			

			
			
		}

		System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
		System.out.println(defs.toString());
		System.out.println(bars.toString());
		System.out.println("</svg>");
		
	}

	
	
	
}
